package com.example.blogdemo.common.Utils;

import ch.qos.logback.core.util.StringUtil;
import com.example.blogdemo.common.exception.BlogException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Map;

@Slf4j
public class JwtUtils {

    private final static String SETCODEBYTE = "ucnzXdg8EmVMFgHWH0bpAQMtL+hYNTuYoegH/gDzQJk=";
    private static Key key = Keys.hmacShaKeyFor(SETCODEBYTE.getBytes(StandardCharsets.UTF_8));


    public static String getJwt(Map map) {


//        Key key = Keys.hmacShaKeyFor(SETCODEBYTE.getBytes(StandardCharsets.UTF_8));
        String compact = Jwts.builder().setClaims(map).signWith(key).compact();



        return compact;
    }

    public static Claims parseJwt(@NotBlank(message = "token不能为空") String s) {


        JwtParser build = Jwts.parserBuilder().setSigningKey(key).build();
        Claims body = null;
        try {
            body = build.parseClaimsJws(s).getBody();

        } catch (Exception e) {
            log.error("token解析失败,token:{}", s);
        }

        return body;
    }
}
